using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class UserHeadCtrl : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "UserHeadCtrl";
		private const SolidColorBrush cBrushCoverNormal = null;
		private const double cDMoveRestrict = 25.0;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(string), typeof(UserHeadCtrl), new PropertyMetadata(null, new PropertyChangedCallback(UserHeadCtrl.OnItemObjectPropertyChanged)));
		private bool mBIsLoaded;
		private static EventWatcher mWatchHeadImg;
		private static object mObjectWatcher = new object();
		private static Dictionary<string, List<UserHeadCtrl>> sDictWaitCtrls = new Dictionary<string, List<UserHeadCtrl>>();
		private object mObject;
		private string mStrUserRequesting;
		private static SolidColorBrush cBrushCoverTouch;
		private bool mBIsClickCancled;
		private static List<UserHeadCtrl> sItems;
		internal Button btnLayoutRoot;
		internal Image imgContent;
		internal Grid gridCover;
		private bool _contentLoaded;
		public event UserHeadCtrl.ListItemClick ItemClick;
		public string ItemObject
		{
			get
			{
				return (string)base.GetValue(UserHeadCtrl.ItemObjectProperty);
			}
			set
			{
				base.SetValue(UserHeadCtrl.ItemObjectProperty, value);
			}
		}
		public UserHeadCtrl()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.UserHeadCtrl_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.UserHeadCtrl_Unloaded));
			ShowObjectInMemory.AddOneObjectRecord(this);
		}
		~UserHeadCtrl()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == UserHeadCtrl.ItemObjectProperty)
			{
				(dependencyObject_0 as UserHeadCtrl).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				if (dependencyPropertyChangedEventArgs_0.get_NewValue() == null)
				{
					this.imgContent.set_Source(HeadImgMgr.defaultHeadImage);
					return;
				}
				this.StartRequest(this.ItemObject);
			}
		}
		private void UserHeadCtrl_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = false;
		}
		private void UserHeadCtrl_Loaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = true;
			this.objectGetted(this.mObject);
			this.mObject = null;
		}
		private static void registerEvent()
		{
			if (UserHeadCtrl.mWatchHeadImg == null)
			{
				UserHeadCtrl.mWatchHeadImg = new EventWatcher(UserHeadCtrl.mObjectWatcher, null, new EventHandlerDelegate(UserHeadCtrl.OnHeadImg));
				EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, UserHeadCtrl.mWatchHeadImg);
			}
		}
		private static void unRegisterEvent()
		{
			if (UserHeadCtrl.mWatchHeadImg != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, UserHeadCtrl.mWatchHeadImg);
				UserHeadCtrl.mWatchHeadImg = null;
			}
		}
		private void ObjectCome(string strUser)
		{
			if (!string.IsNullOrEmpty(strUser) && !(strUser != this.ItemObject))
			{
				this.imgContent.set_Source(HeadImgMgr.getHeadImage(this.ItemObject, true));
				return;
			}
		}
		private void objectGetted(object object_0)
		{
			if (object_0 == null)
			{
				return;
			}
			this.StopRequest();
			if (!this.mBIsLoaded)
			{
				this.mObject = object_0;
				return;
			}
			this.ObjectCome(object_0 as string);
		}
		private static void getOneSnsImg(string strUserName)
		{
			if (strUserName == null)
			{
				return;
			}
			if (UserHeadCtrl.sDictWaitCtrls.ContainsKey(strUserName))
			{
				List<UserHeadCtrl> list = UserHeadCtrl.sDictWaitCtrls.get_Item(strUserName);
				using (List<UserHeadCtrl>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						UserHeadCtrl current = enumerator.get_Current();
						if (current != null)
						{
							current.objectGetted(strUserName);
						}
					}
				}
			}
		}
		private static void addOneCtrlToWait(string strUser, UserHeadCtrl ctrl)
		{
			List<UserHeadCtrl> list;
			if (!UserHeadCtrl.sDictWaitCtrls.ContainsKey(strUser))
			{
				list = new List<UserHeadCtrl>();
				UserHeadCtrl.sDictWaitCtrls.Add(strUser, list);
			}
			else
			{
				list = UserHeadCtrl.sDictWaitCtrls.get_Item(strUser);
			}
			if (!list.Contains(ctrl))
			{
				list.Add(ctrl);
			}
		}
		private static void remOneCtrlFromWait(string strUser, UserHeadCtrl ctrl)
		{
			if (!UserHeadCtrl.sDictWaitCtrls.ContainsKey(strUser))
			{
				return;
			}
			List<UserHeadCtrl> list = UserHeadCtrl.sDictWaitCtrls.get_Item(strUser);
			if (!list.Contains(ctrl))
			{
				list.Remove(ctrl);
			}
			if (list.get_Count() == 0)
			{
				UserHeadCtrl.sDictWaitCtrls.Remove(strUser);
			}
		}
		private static void OnHeadImg(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID != EventConst.ON_MGR_HEAD_IMG_READY)
			{
				return;
			}
			if (evtArgs.mObject == null)
			{
				return;
			}
			if (evtArgs.mObject is List<object>)
			{
				List<object> listObject = evtArgs.getListObject<object>();
				if (listObject == null)
				{
					return;
				}
				using (List<object>.Enumerator enumerator = listObject.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string strUserName = (string)enumerator.get_Current();
						UserHeadCtrl.getOneSnsImg(strUserName);
					}
					return;
				}
			}
			UserHeadCtrl.getOneSnsImg(evtArgs.mObject as string);
		}
		protected void StartRequest(string strUser)
		{
			if (string.IsNullOrEmpty(strUser) || this.mStrUserRequesting == strUser)
			{
				return;
			}
			this.StopRequest();
			this.mStrUserRequesting = strUser;
			if (UserHeadCtrl.mWatchHeadImg == null)
			{
				UserHeadCtrl.registerEvent();
			}
			Contact contact = StorageMgr.contact.method_0(this.mStrUserRequesting);
			BitmapImage headImage;
			if (contact != null)
			{
				headImage = HeadImgMgr.getHeadImage(contact);
			}
			else
			{
				headImage = HeadImgMgr.getHeadImage(this.mStrUserRequesting, true);
			}
			this.imgContent.set_Source(headImage);
			if (headImage != HeadImgMgr.defaultHeadImage)
			{
				this.StopRequest();
				return;
			}
			this.imgContent.set_Source(HeadImgMgr.defaultHeadImage);
			UserHeadCtrl.addOneCtrlToWait(strUser, this);
		}
		public void StopRequest()
		{
			if (this.mStrUserRequesting != null)
			{
				if (this.mStrUserRequesting != null)
				{
					UserHeadCtrl.remOneCtrlFromWait(this.mStrUserRequesting, this);
				}
				this.mStrUserRequesting = null;
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
		}
		private void clicked()
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, null);
			}
			Contact contact = StorageMgr.contact.method_0(this.ItemObject);
			if (contact == null)
			{
				contact = new Contact();
				contact.strUsrName = this.ItemObject;
			}
			object[] passData = new object[]
			{
				contact
			};
			SnsUserMainPage.GoToThisPage(passData);
		}
		private void Grid_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			this.mBIsClickCancled = false;
			this.gridCover.set_Background(UserHeadCtrl.cBrushCoverTouch);
		}
		private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			Point translation = e.get_DeltaManipulation().get_Translation();
			if (translation.get_X() * translation.get_X() + translation.get_Y() * translation.get_Y() > 25.0)
			{
				this.gridCover.set_Background(null);
				this.mBIsClickCancled = true;
			}
		}
		private void Grid_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			this.gridCover.set_Background(null);
			if (!this.mBIsClickCancled)
			{
				this.clicked();
			}
		}
		public static UserHeadCtrl GetOneItem()
		{
			if (UserHeadCtrl.sItems != null && UserHeadCtrl.sItems.get_Count() != 0)
			{
				UserHeadCtrl userHeadCtrl = Enumerable.First<UserHeadCtrl>(UserHeadCtrl.sItems);
				UserHeadCtrl.clearItemParent(userHeadCtrl);
				UserHeadCtrl.sItems.RemoveAt(0);
				return userHeadCtrl;
			}
			return new UserHeadCtrl();
		}
		public static int GetCacheCnt()
		{
			if (UserHeadCtrl.sItems == null)
			{
				return 0;
			}
			return UserHeadCtrl.sItems.get_Count();
		}
		public static void PushOneItem(UserHeadCtrl item)
		{
			if (item == null)
			{
				return;
			}
			item.set_Visibility(0);
			if (UserHeadCtrl.sItems == null)
			{
				UserHeadCtrl.sItems = new List<UserHeadCtrl>();
			}
			UserHeadCtrl.sItems.Add(item);
		}
		private static void clearItemParent(UserHeadCtrl item)
		{
			if (item.get_Parent() != null)
			{
				Panel panel = item.get_Parent() as Panel;
				if (panel != null)
				{
					panel.get_Children().Remove(item);
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/UserHeadCtrl.xaml", 2));
			this.btnLayoutRoot = (Button)base.FindName("btnLayoutRoot");
			this.imgContent = (Image)base.FindName("imgContent");
			this.gridCover = (Grid)base.FindName("gridCover");
		}
		static UserHeadCtrl()
		{
			// Note: this type is marked as 'beforefieldinit'.
			SolidColorBrush solidColorBrush = new SolidColorBrush();
			solidColorBrush.set_Color(Color.FromArgb(153, 0, 0, 0));
			UserHeadCtrl.cBrushCoverTouch = solidColorBrush;
			UserHeadCtrl.sItems = new List<UserHeadCtrl>();
		}
	}
}
